package com.europe.business.europebusiness.ui.fragment.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.europe.business.europebusiness.R;

public abstract class ActionBarFragment extends BaseFragment {

    private Toolbar mToolbar;
    private LinearLayout mContent;
    private TextView title;
    private ImageView leftImg;
    private TextView reg;
    private ImageView rightImg;

    @Override
    protected int setLayout() {
        return R.layout.fragment_actionbar;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    protected void initView(View view) {
        initToolbar(view);
        mContent = view.findViewById(R.id.fragment_content);

        int layout = setMainContent();
        View contentView = LayoutInflater.from(context).inflate(layout, mContent, false);
        mContent.addView(contentView);

    }

    protected void initToolbar(View view){
        mToolbar = view.findViewById(R.id.toolbar);
        reg = view.findViewById(R.id.reg);
        rightImg = view.findViewById(R.id.right_img);
        leftImg = view.findViewById(R.id.left_img);
        title = view.findViewById(R.id.title);
        ((AppCompatActivity)context).setSupportActionBar(mToolbar);
        setActionbarBackground();
        mToolbar.setTitle("");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToastShort("aaa");
            }
        });

        rightImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRightImgClick();
            }
        });

    }


    /**
     * 设置右边图片的图标以及可见性
     * @param str
     */
    protected void setToolBarRightText(int visible, String str){
        reg.setVisibility(visible);
        if(visible == View.VISIBLE){
            reg.setText(str);
        }

    }


    /**
     * 设置右边图片的图标以及可见性
     * @param res
     */
    protected void setToolBarRightImage(int visible, int res){
        rightImg.setVisibility(visible);
        if(visible == View.VISIBLE){
            rightImg.setImageResource(res);
        }

    }


    /**
     * 设置左边图片的图标以及可见性
     * @param res
     */
    protected void setToolBarLeftImage(int visible, int res){
        leftImg.setVisibility(visible);
        if(visible == View.VISIBLE){
            leftImg.setImageResource(res);
        }
    }

    /**
     * 子类设置返回键的图标
     * @param drawable
     */
    protected void setToolbarNavigationIcon(int drawable){
        mToolbar.setNavigationIcon(drawable);
    }

    /**
     * 隐藏actionbar（toolbar）
     */
    protected void setToolbarVisible(int visible){
        mToolbar.setVisibility(visible);
    }


    /**
     *设置全屏幕(透明状态栏)
     */
    protected void setFullScreen(){
        if (Build.VERSION.SDK_INT >= 21) {
            ((Activity)context).getWindow().setNavigationBarColor(Color.RED);
            ((Activity)context).getWindow().setStatusBarColor(Color.RED);
        }
    }

    protected void setWindowStatusBarColor(int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ((Activity)context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                ((Activity)context).getWindow().setStatusBarColor(getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 子类设置标题
     * @param titleStr
     */
    protected void setTitle(String titleStr){
        title.setText(titleStr);
    }

    protected void setActionbarBackground(){
        mToolbar.setBackgroundColor(getResources().getColor(R.color.white));
    }

    protected abstract int setMainContent();

    /**
     * 设置右边图片的图标点击事件
     */
    protected void setRightImgClick(){}
}
